/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: MapPanel.java 648 2010-07-30 06:40:14Z Harry $
 *   $LastChangedDate: 2010-07-30 06:40:14 +0000 (Fri, 30 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.component;

import battlefield.entity.Map;
import battlefield.entity.Unit;
import battlefield.state.UnitType;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

/**
 *
 * @author Harry Nguyen
 */
public class MapPanel extends JPanel implements MouseListener {

    private MapButton[][] buttons;
    private boolean unitVisibility = true;
    private Map map;
    private Unit previewUnit;

    public MapPanel(Map map, boolean unitVisibility) {
        this.map = map;
        this.unitVisibility = unitVisibility;
        initComponents();
    }

    private void initComponents() {
        setPreferredSize(new Dimension(map.getWidth(), map.getHeight()));
        setLayout(new GridLayout(map.getRows(), map.getColumns()));

        buttons = new MapButton[map.getRows()][map.getColumns()];

        for (int i = 0; i < map.getRows(); i++) {
            for (int j = 0; j < map.getColumns(); j++) {
                buttons[i][j] = new MapButton();
                buttons[i][j].setRow(i);
                buttons[i][j].setColumn(j);
                buttons[i][j].setUnitVisibility(unitVisibility);
                buttons[i][j].addMouseListener(this);
                this.add(buttons[i][j]);
            }
        }
        update(map);
    }

    public boolean isUnitVisibility() {
        return unitVisibility;
    }

    public void setUnitVisibility(boolean unitVisibility) {
        this.unitVisibility = unitVisibility;
    }

    public Unit getPreviewUnit() {
        return previewUnit;
    }

    public void setPreviewUnit(Unit previewUnit) {
        this.previewUnit = previewUnit;
    }

    public void revealDestroyedUnit(Unit unit) {
        for (int i = 0; i < unit.getRows(); i++) {
            for (int j = 0; j < unit.getColumns(); j++) {
                buttons[unit.getX() + i][unit.getY() + j].setUnitVisibility(true);
                buttons[unit.getX() + i][unit.getY() + j].update();
            }
        }
    }

    public void update(Map map) {
        for (int i = 0; i < map.getRows(); i++) {
            for (int j = 0; j < map.getColumns(); j++) {
                buttons[i][j].setCell(map.getCellAt(i, j));
            }
        }
    }

    public void addActionListener(ActionListener listener, String actionCommand) {
        for (int i = 0; i < buttons.length; i++) {
            for (int j = 0; j < buttons[i].length; j++) {
                buttons[i][j].addActionListener(listener);
                buttons[i][j].setActionCommand(actionCommand);
            }
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        if (e.getSource() instanceof MapButton) {
            MapButton button = (MapButton) e.getSource();
            if (getPreviewUnit() == null || button.isPreview()) {
                return;
            }

            for (int i = 0; i < getPreviewUnit().getRows(); i++) {
                for (int j = 0; j < getPreviewUnit().getColumns(); j++) {
                    int bx = button.getRow() + i;
                    int by = button.getColumn() + j;
                    if (getPreviewUnit().hasCellAt(i, j) && (bx < buttons.length) && (by < buttons[bx].length)) {
                        buttons[bx][by].setPreview(true);
                        buttons[bx][by].setPreviewCell(getPreviewUnit().getCellAt(i, j));
                        buttons[bx][by].setErrorVisibility(getPreviewUnit().getType() != UnitType.TOOL);
                        buttons[bx][by].update();
                    }
                }
            }
        }
    }

    public void mouseExited(MouseEvent e) {
        if (e.getSource() instanceof MapButton) {
            MapButton button = (MapButton) e.getSource();
            if (getPreviewUnit() == null) {
                return;
            }

            for (int i = 0; i < getPreviewUnit().getRows(); i++) {
                for (int j = 0; j < getPreviewUnit().getColumns(); j++) {
                    int bx = button.getRow() + i;
                    int by = button.getColumn() + j;
                    if ((bx < buttons.length) && (by < buttons[bx].length)) {
                        buttons[bx][by].setPreview(false);
                        buttons[bx][by].setPreviewCell(null);
                        buttons[bx][by].setErrorVisibility(getPreviewUnit().getType() == UnitType.TOOL);
                        buttons[bx][by].update();
                    }
                }
            }
        }
    }
}
